(function ($) {
	AjaxSolr.CurrentSearchWidget = AjaxSolr.AbstractWidget.extend({
		proteins: Array(), 
		numOfInteracts:{},
		coloring:false,
		afterRequest: function () {
			var self = this;
			var links = [];
			
			if (typeof this.manager.response.responseHeader.params.q != 'undefined')
				self.numOfInteracts[this.manager.response.responseHeader.params.q.substr(5)]=this.manager.response.response.docs.length;
			
			if (self.proteins.length>0) {
				for (var i = 0, l = self.proteins.length; i < l; i++) {
					links.push($('<a href="#"  />').html(' <img src="css/delete.png" />').click(self.removeFacet(self.proteins[i])));
				}

				if (links.length > 1) {
					links.push($('<a href="#"/>').text('remove all').click(function () {
						self.proteins= Array();
						self.manager.widgets["graph"].resetGraphic();
						if (self.manager.store.addByValue('q', "*:*")) {
							self.manager.doRequest(0);
						}
						return false;
					}));
				}
				$(this.target).empty();
				for (var i = 0, l = links.length; i < l; i++) {
					var item = $("<div class='item' >").appendTo($(this.target)); 
					if (i<links.length-1 || links.length==1){
						var color = self.manager.widgets["graph"].getColor(i);
						item.append(self.proteins[i]);
						if (typeof self.numOfInteracts[self.proteins[i]] != 'undefined') item.append(' ('+self.numOfInteracts[self.proteins[i]]+')');
						item.append(" <span style='background-color: "+color+"; color: "+color+";'>__</span> ");
					}
					item.append(links[i]);
				}
//				var checked=(self.coloring)?"checked":"";
//				var colorLink = $('<input type="checkbox" value="Color" id="color" '+checked+'/>');
//				colorLink.change(function () {
//					if (typeof $(this).attr("checked") != 'undefined'){
//						self.manager.widgets["graph"].registerStyler("proteinSeed",function () {self.manager.widgets["graph"].colorBySeed(self.manager.widgets["graph"]);});
//					}else{
//						self.manager.widgets["graph"].removeStyler("proteinSeed");
//					}
//					self.manager.widgets["graph"].executeStylers();
//				});
//				$(this.target).append(colorLink);
//				$(this.target).append('<label for="color"> Color By Protein</label>');

			} else {
				$(this.target).html('<div class="item">Random selection.</div>');
			}
		},

		removeFacet: function (facet) {
			var self = this; 
			return function () {
				var index = jQuery.inArray(facet,self.proteins);
				if (index==-1) return;
				self.proteins.splice(index, 1);
				self.afterRequest();
				if (facet.indexOf("*")==0) {
					facet=facet.substr(1);
					self.manager.widgets["graph"].removeProteinForce(facet);
				}else
					self.manager.widgets["graph"].removeProtein(facet,self.proteins);
				self.manager.widgets["ruler"].afterRequest();
				
				if (self.proteins.length==0 && self.manager.store.addByValue('q', "*:*")) 
					self.manager.doRequest(0);
				
				return false;
			};
		},
		requestInteractionsByProtein: function(protein){
			var self =this;
			if (jQuery.inArray(protein,self.proteins)!=-1) return false;
			var pos=jQuery.inArray("*"+protein,self.proteins);
			if (pos!=-1)
				self.proteins[pos]=protein;
			else
				self.proteins.push(protein);
			var q=self.manager.store.get('q').val();
			q = 'text:'+protein;
			if (self.manager.store.addByValue('q', q)) {
				self.manager.doRequest(0);
			}
			return true;
		},
		requestSingleProtein: function(protein){
			var self =this;
			if (jQuery.inArray(protein,self.proteins)!=-1) return false;
			self.proteins.push("*"+protein);
			var q=self.manager.store.get('q').val();
			q = 'text:'+protein;
			self.manager.store.addByValue('rows', 1);
			if (self.manager.store.addByValue('q', q)) {
				self.manager.doRequest(0);
				self.manager.store.addByValue('rows', self.manager.limitPerQuery);
			}
			return true;
		},
		requestInteractionsBetweenProteins: function(protein,interactors){
			var self =this;
			if (jQuery.inArray(protein,self.proteins)!=-1) return false;
			var q=self.manager.store.get('q').val();
			var sep="";
			var interStr="";
			for (var i=0; i< interactors.length; i++){
				interStr += sep+"text:"+interactors[i];
				sep=" OR ";
			}
			q = 'text:'+protein;
			if (self.manager.widgets["graph"].previousRequest!="*:*"){
				for (var i=0; i< self.manager.widgets["graph"].graph.proteins.length; i++){
					interStr += sep+"text:"+self.manager.widgets["graph"].graph.proteins[i].id;
					sep=" OR ";
				}
			}
			if (interStr!="") q+=' AND ('+interStr+')';
			else return false;
			if (self.manager.store.addByValue('q', q)) {
				self.manager.doRequest(0);
			}
			return true;
		}
	});
})(jQuery);